import { EventMouse } from 'cc';
import { Input, input, EventTouch, v2, Vec2, math, log, screen } from 'cc';
import { Observable, Observer, filter } from "rxjs";

/**
 * 在input上注册Mouse事件不好使，不知原因。
 * 请使用BindNode中的BindNodeMouse
 */

export function MouseDownUp() {
    return new Observable((observer: Observer<EventMouse>) => {
        const onMouseDown = (event: EventMouse) => observer.next(event);
        const onMouseUp = (event: EventMouse) => observer.next(event);
        input.on(Input.EventType.MOUSE_DOWN, onMouseDown, this);
        input.on(Input.EventType.MOUSE_UP, onMouseUp, this);
        return () => {
            input.off(Input.EventType.MOUSE_DOWN, onMouseDown, this);
            input.off(Input.EventType.MOUSE_UP, onMouseUp, this);
        }
    });
}

export function MouseMove() {
    return new Observable((observer: Observer<EventMouse>) => {
        const onMouseMove = (eventMouse: EventMouse) => {
            observer.next(eventMouse);
        };
        input.on(Input.EventType.MOUSE_MOVE, onMouseMove, this);
        return () => {
            input.off(Input.EventType.MOUSE_MOVE, onMouseMove, this);
        }
    });
}

export function MouseWheel() {
    return new Observable((observer: Observer<EventMouse>) => {
        const onMouseWheel = (eventMouse: EventMouse) => {
            observer.next(eventMouse);
        };
        input.on(Input.EventType.MOUSE_WHEEL, onMouseWheel, this);
        return () => {
            input.off(Input.EventType.MOUSE_WHEEL, onMouseWheel, this);
        }
    })
}